package com.xci.sys.api;

import com.xci.core.annotation.Authorize;
import com.xci.core.annotation.EncryptResponseBody;
import com.xci.core.base.ApiController;
import com.xci.core.base.PageList;
import com.xci.core.base.RestMessage;
import com.xci.core.domain.PageModel;
import com.xci.core.helper.ExcelHelper;
import com.xci.core.helper.Helper;
import com.xci.core.internal.Const;
import com.xci.sys.entity.SysDept;
import com.xci.sys.entity.SysModule;
import com.xci.sys.entity.SysRole;
import com.xci.sys.filter.RoleFilter;
import com.xci.sys.service.RoleService;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 角色接口
 * @author 吕艳阳
 */
@ApiSort(4)
@Api(tags = "角色接口")
@Authorize
@EncryptResponseBody
@RestController
@RequestMapping(value = "/api/sys/role", produces = Const.P_JSON)
public class RoleApiController extends ApiController {
    @Resource
    private RoleService roleService;//角色服务

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "新建角色")
    @Authorize(code = "sys.role.insert")
    @PostMapping("/insert")
    public RestMessage insert(@RequestBody SysRole entity) {
        return roleService.insert(entity);
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "修改角色")
    @Authorize(code = "sys.role.update")
    @PostMapping("/update")
    public RestMessage update(@RequestBody SysRole entity) {
        return roleService.update(entity);
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "修改角色状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "角色主键字符串", required = true),
            @ApiImplicitParam(name = "status", value = "角色状态", required = true)
    })
    @Authorize(code = "sys.role.updateStatus")
    @PostMapping("/updateStatus")
    public RestMessage updateStatus(String ids, Boolean status) {
        return roleService.updateStatus(ids, status);
    }

    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "批量保存(新建或者更新)角色对象")
    @PostMapping("/batchSave")
    public RestMessage batchSave(@RequestBody List<SysRole> entities) {
        return roleService.batchSave(entities);
    }

    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "删除角色")
    @ApiImplicitParam(name = "ids", value = "角色主键字符串", required = true)
    @Authorize(code = "sys.role.delete")
    @PostMapping("/delete")
    public RestMessage delete(String ids) {
        return roleService.delete(ids);
    }

    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "检查角色编码是否存在")
    @ApiImplicitParam(name = "code", value = "角色编码", required = true)
    @PostMapping("/existByCode/{code}")
    public RestMessage existByCode(@PathVariable String code) {
        return RestMessage.result(roleService.existByCode(code, null));
    }

    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "查询单个角色")
    @ApiImplicitParam(name = "id", value = "角色主键", required = true)
    @PostMapping("/selectById/{id}")
    public RestMessage<SysRole> selectById(@PathVariable String id) {
        return RestMessage.success(roleService.selectById(id));
    }

    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "根据编码查询单个角色")
    @ApiImplicitParam(name = "code", value = "角色编码", required = true)
    @PostMapping("/selectByCode/{code}")
    public RestMessage<SysRole> selectByCode(@PathVariable String code) {
        return RestMessage.success(roleService.selectByCode(code));
    }

    @ApiOperationSupport(order = 9)
    @ApiOperation(value = "根据用户主键查询单个角色")
    @ApiImplicitParam(name = "userId", value = "用户主键", required = true)
    @PostMapping("/selectByUserId/{userId}")
    public RestMessage<SysRole> selectByUserId(@PathVariable String userId) {
        return RestMessage.success(roleService.selectByUserId(userId));
    }

    @ApiOperationSupport(order = 10)
    @ApiOperation(value = "查询角色分页列表")
    @PostMapping("/selectPageList")
    public RestMessage<PageList<SysRole>> selectPageList(RoleFilter filter, PageModel p) {
        return RestMessage.success(PageList.of(roleService.selectPageList(filter)));
    }

    @ApiOperationSupport(order = 11)
    @ApiOperation(value = "查询角色列表")
    @PostMapping("/selectList")
    public RestMessage<List<SysRole>> selectList(RoleFilter filter) {
        return RestMessage.success(roleService.selectList(filter));
    }

    @ApiOperationSupport(order = 12)
    @ApiOperation(value = "导出角色列表")
    @Authorize(code = "sys.role.export")
    @ApiImplicitParam(name = "fieldNames", value = "导出字段名字符串")
    @PostMapping(value = "/export", produces = {Const.P_OCTET, Const.P_JSON})
    public void export(RoleFilter filter, @RequestParam(required = false) String fieldNames) {
        ExcelHelper.exportWeb(roleService.selectList(filter), SysRole.class, "系统角色列表", fieldNames);
    }

    @ApiOperationSupport(order = 13)
    @ApiOperation(value = "获取角色导出字段")
    @PostMapping(value = "/exportNames")
    public RestMessage<List<String>> exportNames() {
        return RestMessage.success(ExcelHelper.getExcelFiledNames(SysRole.class));
    }

    @ApiOperationSupport(order = 13)
    @ApiOperation(value = "刷新角色缓存")
    @PostMapping(value = "/refreshCache")
    public RestMessage refreshCache() {
        roleService.refreshCache();
        return RestMessage.success();
    }

    @ApiOperationSupport(order = 14)
    @ApiOperation(value = "保存角色权限")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleIds", value = "角色主键字符串"),
            @ApiImplicitParam(name = "deptScope", value = "数据范围 [全部_1,所在机构_2,所在机构及所有下级_3,自定义_4,仅本人_5]"),
            @ApiImplicitParam(name = "deptIds", value = "机构主键数组字符串"),
            @ApiImplicitParam(name = "moduleIds", value = "模块主键数组字符串")
    })
    @PostMapping("/savePermission")
    public RestMessage savePermission(String roleIds, Integer deptScope, String deptIds, String moduleIds) {
        String[] roleIdList = Helper.splitToArray(roleIds);
        for (String roleId : roleIdList) {
            roleService.saveModuleMap(roleId, Helper.splitToArray(moduleIds));
            roleService.saveDeptMap(roleId, deptScope, Helper.splitToArray(deptIds));
        }
        return RestMessage.success();
    }

    @ApiOperationSupport(order = 14)
    @ApiOperation(value = "保存角色关联模块")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色主键"),
            @ApiImplicitParam(name = "moduleIds", value = "模块主键数组字符串")
    })
    @PostMapping("/saveModuleMap")
    public RestMessage saveModuleMap(String roleId, String moduleIds) {
        return roleService.saveModuleMap(roleId, Helper.splitToArray(moduleIds));
    }

    @ApiOperationSupport(order = 15)
    @ApiOperation(value = "根据角色主键删除关联模块")
    @ApiImplicitParam(name = "roleId", value = "角色主键")
    @PostMapping("/deleteModuleMap/{roleId}")
    public RestMessage deleteModuleMap(@PathVariable String roleId) {
        return roleService.deleteModuleMap(roleId);
    }

    @ApiOperationSupport(order = 16)
    @ApiOperation(value = "根据角色主键查询关联模块主键集合")
    @ApiImplicitParam(name = "roleId", value = "角色主键")
    @PostMapping("/selectModuleMapArray/{roleId}")
    public RestMessage<List<String>> selectModuleMapArray(@PathVariable String roleId) {
        return RestMessage.success(roleService.selectModuleMapArray(roleId));
    }

    @ApiOperationSupport(order = 17)
    @ApiOperation(value = "根据角色主键查询关联模块对象列表")
    @ApiImplicitParam(name = "roleId", value = "角色主键")
    @PostMapping("/selectModuleMapList/{roleId}")
    public RestMessage<List<SysModule>> selectModuleMapList(@PathVariable String roleId) {
        return RestMessage.success(roleService.selectModuleMapList(roleId));
    }

    @ApiOperationSupport(order = 18)
    @ApiOperation(value = "保存角色关联机构")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色主键"),
            @ApiImplicitParam(name = "deptScope", value = "数据范围 [全部_1,所在机构_2,所在机构及所有下级_3,自定义_4,仅本人_5]"),
            @ApiImplicitParam(name = "moduleIds", value = "机构主键数组字符串")
    })
    @PostMapping("/saveDeptMap")
    public RestMessage saveDeptMap(String roleId, Integer deptScope, String deptIds) {
        return roleService.saveDeptMap(roleId, deptScope, Helper.splitToArray(deptIds));
    }

    @ApiOperationSupport(order = 19)
    @ApiOperation(value = "根据角色主键删除关联机构")
    @ApiImplicitParam(name = "roleId", value = "角色主键")
    @PostMapping("/deleteDeptMap/{roleId}")
    public RestMessage deleteDeptMap(@PathVariable String roleId) {
        return roleService.deleteDeptMap(roleId);
    }

    @ApiOperationSupport(order = 20)
    @ApiOperation(value = "根据角色主键查询关联机构主键集合")
    @ApiImplicitParam(name = "roleId", value = "角色主键")
    @PostMapping("/selectDeptMapArray/{roleId}")
    public RestMessage<List<String>> selectDeptMapArray(@PathVariable String roleId) {
        return RestMessage.success(roleService.selectDeptMapArray(roleId));
    }

    @ApiOperationSupport(order = 21)
    @ApiOperation(value = "根据角色主键查询关联机构对象列表")
    @ApiImplicitParam(name = "roleId", value = "角色主键")
    @PostMapping("/selectDeptMapList/{roleId}")
    public RestMessage<List<SysDept>> selectDeptMapList(@PathVariable String roleId) {
        return RestMessage.success(roleService.selectDeptMapList(roleId));
    }
}